/**Created by hejg on 2018/5/9.*/
import React, {Component} from 'react';
import {
    View,
    Text,
    ActivityIndicator,
    StatusBar,
    AsyncStorage,
    StyleSheet,
    Alert,
    Platform
} from 'react-native';
import codePush from "react-native-code-push";
import JPushModule from 'jpush-react-native';
import Style from '../../../components/Style';
import Tool from '../../../components/Tool';

@codePush({})
export default class Load extends Component {
    constructor(props) {
        super(props);
        this.state = {codePushMessage: ''};
    }

    componentDidMount() {
        if (Platform.OS === 'android') {
            JPushModule.initPush()
            JPushModule.notifyJSDidLoad(resultCode => {
                if (resultCode === 0) {
                }
            })
        } else {
            JPushModule.setupPush();
        }

        JPushModule.addReceiveOpenNotificationListener(map => {
            console.log('Opening notification!');
            console.log('map.extra: ' + map.extras);
            // this.jumpSecondActivity();
            // JPushModule.jumpToPushActivity("SecondActivity");
        })
    }

    componentWillUnmount() {
        // JPushModule.removeReceiveOpenNotificationListener(() => {});
        // console.warn('Will clear all notifications');
        // JPushModule.clearAllNotifications();
    }

    codePushStatusDidChange(status) {
        switch (status) {
            case codePush.SyncStatus.CHECKING_FOR_UPDATE:
                this.setState({codePushMessage: "正在查询CodePush服务器的更新."});
                break;
            case codePush.SyncStatus.DOWNLOADING_PACKAGE:
                this.setState({codePushMessage: "正在从CodePush服务器下载可用更新."});
                break;
            case codePush.SyncStatus.INSTALLING_UPDATE:
                this.setState({codePushMessage: "可用的更新已下载并即将安装."});
                break;
            case codePush.SyncStatus.UP_TO_DATE:
                this.setState({codePushMessage: "该应用程序与CodePush服务器保持同步."});
                this._bootstrapAsync();
                break;
            case codePush.SyncStatus.UPDATE_INSTALLED:
                this.setState({codePushMessage: "已安装可用更新."});
                break;
            case codePush.SyncStatus.UNKNOWN_ERROR:
                this.setState({codePushMessage: "同步操作遇到未知错误."});
                break;
        }
    }

    _bootstrapAsync() {
        let _this = this;
        AsyncStorage.getItem(Tool.storageConst.userKey, (error, result) => {
            if (result) {//如果本地记录了登陆用户信息
                try {
                    let user = JSON.parse(result);
                    _this.props.navigation.navigate('Gesture');
                } catch (err) {//本地存储的不是合法的用户信息
                    _this.props.navigation.navigate('Auth');
                }
            } else {//本地没有记录登陆用户信息
                _this.props.navigation.navigate('Auth');
            }
        });
    }

    render() {
        return (
            <View style={styles.container}>
                <StatusBar backgroundColor={Style.color.white} barStyle="dark-content"/>
                <ActivityIndicator size="small" color={Style.color.gray}/>
                <Text style={styles.codePushMessage}>{this.state.codePushMessage}</Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
        backgroundColor: Style.color.white
    },
    codePushMessage: {
        fontSize: Style.fontSize.normal,
        color: Style.color.black
    }
});